import json
from time import sleep
from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://www.baidu.com/")
driver.maximize_window()
driver.find_element_by_id("s-top-loginbtn").click()
sleep(1)
driver.find_element_by_id("TANGRAM__PSP_11__userName").send_keys("18718658584")
driver.find_element_by_id("TANGRAM__PSP_11__password").send_keys("W1234567")
sleep(1)
driver.find_element_by_id("TANGRAM__PSP_11__submit").click()
sleep(3)
# print(driver.get_cookies())
# for cookie in driver.get_cookies():
#     print("%s/%s/%s" % (cookie['name'], cookie['domain'], cookie['value']))

# BDUSS绕过认证进行登录
driver.add_cookie({"domain": "www.baidu.com", "name": "BDUSS", "value": "012eDVJbEVtd1NZYW9EVH53M3ZGSnNQb1lIRVdLV2UzLWdyYkRoYjE0RW5LMWhsSUFBQUFBJCQAAAAAAQAAAAEAAACt5dgjAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACeeMGUnnjBlU"})
driver.refresh()
# cookie保存为json文件
with open("cookie.txt", "w") as cookief:
    cookief.write(json.dumps(driver.get_cookies()))

driver.delete_all_cookies()
driver.refresh()
sleep(2)
# 载入保存的文件
with open("cookie.txt", "r") as cookief:
    cookieList = json.load(cookief)
    for cookie in cookieList:
        driver.add_cookie(cookie)
driver.refresh()
sleep(5)
driver.quit()




